package jp.ac.u_tokyo.dch.action.conv;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import jp.ac.u_tokyo.dch.core.db.DBConnection;
import jp.ac.u_tokyo.dch.core.exceptions.DBException;
import jp.ac.u_tokyo.dch.dao.data.DataDao;
import jp.ac.u_tokyo.dch.dao.schema.DataSchemaDao;
import jp.ac.u_tokyo.dch.model.DataGroup;
import jp.ac.u_tokyo.dch.model.SchemaGroup;
import jp.ac.u_tokyo.dch.model.output.Attribute;
import jp.ac.u_tokyo.dch.service.ConvService;
import jp.ac.u_tokyo.dch.service.DaoServiceAware;
import jp.ac.u_tokyo.dch.util.common.CommonUtil;

import org.apache.commons.lang.xwork.StringUtils;

import com.opensymphony.xwork2.ActionSupport;

public class ConvXmlDownLoadAction extends ActionSupport implements DaoServiceAware {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private String contentType;
	private Long contentLength;
	private String contentDisposition;
	private String inputName;
	private String bufferSize;
	private InputStream inputStream;

	private String lv1Dataid;
	private String lv2Dataid;
	private String lv3Dataid;
	private String keySchemaid;
	private String version;
	private String encoding;
	private String encodingOther;

	private String rootTagName;
	private List<Attribute> rootAttrList = new ArrayList<Attribute>();

	private String deleteIndex;

	private List<DataGroup> dataGroupList;
	/**
	 * DBコネクションオブジェクト
	 */
	private DBConnection dbConnection = null;

	public String init() throws Exception {
		initProcess();
		rootTagName = "ead";

		Attribute defalut = new Attribute();
		defalut.setName("audience");
		defalut.setValue("external");
		rootAttrList.add(defalut);
		return SUCCESS;
	}

	public String addAttr() throws Exception {
		initProcess();
		rootAttrList.add(new Attribute());

		return SUCCESS;
	}
	public String delAttr() throws Exception {
		initProcess();
		rootAttrList.remove(Integer.valueOf(deleteIndex).intValue());

		return SUCCESS;
	}

	public String download() throws Exception {

		if (StringUtils.isEmpty(rootTagName)) {
			addFieldError("rootTagName", getText("requiredstring", new String[] { "タグ名" }));
		}

		if (CommonUtil.isContansXmlChar(rootTagName)) {
			addFieldError("rootTagName", getText("xmlescapemozierror", new String[] { "タグ名" }));
		}
		int i = 0;
		for (Attribute attr : rootAttrList) {

			if (StringUtils.isEmpty(attr.getName())) {
				addFieldError("rootAttrList[" + i + "].name", getText("requiredstring", new String[] { "属性名" }));
			}
			if (StringUtils.isEmpty(attr.getValue())) {
				addFieldError("rootAttrList[" + i + "].value", getText("requiredstring", new String[] { "属性値" }));
			}
			if (CommonUtil.isContansXmlChar(attr.getName())) {
				addFieldError("rootAttrList[" + i + "].name", getText("xmlescapemozierror", new String[] { "属性名" }));
			}
			if (CommonUtil.isContansXmlCharSpaceYes(attr.getValue())) {
				addFieldError("rootAttrList[" + i + "].value", getText("xmlescapemozierror", new String[] { "属性値" }));
			}
			i++;
		}

		if (hasErrors()) {
			initProcess();
			return ERROR;
		}

		DataSchemaDao dataSchemaDao = new DataSchemaDao(dbConnection);
		ConvService service = new ConvService(dbConnection);

		SchemaGroup schemaGroup = dataSchemaDao.selectMetaDataPattern(keySchemaid);

		String enc = "UTF-8";

		if ("0".equals(encoding)) {
			// 入力ストリームを定義
			enc = encodingOther;
		} else {
			enc = encoding;
		}

		String outputXml = service.outputXml(schemaGroup, lv1Dataid, lv2Dataid, lv3Dataid, version, enc, rootTagName, rootAttrList);

		// 入力ストリームを定義
		setInputStream(new ByteArrayInputStream(outputXml.getBytes(enc)));

		// ダウンロード時のファイル名を定義
		setContentDisposition("filename=\"output.xml\"");

		// ダウンロード処理を実行
		return SUCCESS;
	}

	@Override
	public void setDBConnection(DBConnection con) {
		dbConnection = con;

	}

	private void initProcess() throws DBException {

		DataDao dataDao = new DataDao(dbConnection);
		dataGroupList = dataDao.selectDataGroupByL3(keySchemaid);

	}
	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public Long getContentLength() {
		return contentLength;
	}

	public void setContentLength(Long contentLength) {
		this.contentLength = contentLength;
	}

	public String getContentDisposition() {
		return contentDisposition;
	}

	public void setContentDisposition(String contentDisposition) {
		this.contentDisposition = contentDisposition;
	}

	public String getInputName() {
		return inputName;
	}

	public void setInputName(String inputName) {
		this.inputName = inputName;
	}

	public String getBufferSize() {
		return bufferSize;
	}

	public void setBufferSize(String bufferSize) {
		this.bufferSize = bufferSize;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public DBConnection getDbConnection() {
		return dbConnection;
	}

	public void setDbConnection(DBConnection dbConnection) {
		this.dbConnection = dbConnection;
	}

	public String getLv1Dataid() {
		return lv1Dataid;
	}

	public void setLv1Dataid(String lv1Dataid) {
		this.lv1Dataid = lv1Dataid;
	}

	public String getLv2Dataid() {
		return lv2Dataid;
	}

	public void setLv2Dataid(String lv2Dataid) {
		this.lv2Dataid = lv2Dataid;
	}

	public String getLv3Dataid() {
		return lv3Dataid;
	}

	public void setLv3Dataid(String lv3Dataid) {
		this.lv3Dataid = lv3Dataid;
	}

	public String getKeySchemaid() {
		return keySchemaid;
	}

	public void setKeySchemaid(String keySchemaid) {
		this.keySchemaid = keySchemaid;
	}

	public String getEncoding() {
		return encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}

	public String getEncodingOther() {
		return encodingOther;
	}

	public void setEncodingOther(String encodingOther) {
		this.encodingOther = encodingOther;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getRootTagName() {
		return rootTagName;
	}

	public void setRootTagName(String rootTagName) {
		this.rootTagName = rootTagName;
	}

	public List<Attribute> getRootAttrList() {
		return rootAttrList;
	}

	public void setRootAttrList(List<Attribute> rootAttrList) {
		this.rootAttrList = rootAttrList;
	}

	public String getDeleteIndex() {
		return deleteIndex;
	}

	public void setDeleteIndex(String deleteIndex) {
		this.deleteIndex = deleteIndex;
	}

	public List<DataGroup> getDataGroupList() {
		return dataGroupList;
	}

	public void setDataGroupList(List<DataGroup> dataGroupList) {
		this.dataGroupList = dataGroupList;
	}

}
